#include "connector.h"
#include <QLabel>
#include <QtGui>
#include <QPushButton>
#include "erl3.h"
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <QErrorMessage>
#include <fstream>
#include <QGraphicsScene>
#include "hexitem.h"

Connector::Connector(QWidget *parent) :
    QDialog(parent)
{

    common = new Common();
    myerl = new Myerl();
    myerl->common = common;
    QLabel *nameLabel = new QLabel(tr("Nazwa:"));
    QLabel *hostLabel = new QLabel(tr("Host:"));
    QLabel *cookieLabel = new QLabel(tr("Ciasteczko:"));
    actCancel = new QPushButton("&Anuluj");
    actConnect = new QPushButton("&Połącz");
    loadDefault();
    QGridLayout *mainLayout = new QGridLayout;
    mainLayout->addWidget(nameLabel, 0, 0);
    mainLayout->addWidget(name, 0, 1);
    mainLayout->addWidget(hostLabel, 1, 0);
    mainLayout->addWidget(host, 1, 1);
    mainLayout->addWidget(cookieLabel, 2, 0);
    mainLayout->addWidget(cookie, 2, 1);
    mainLayout->addWidget(actCancel,3,0);
    mainLayout->addWidget(actConnect,3,1);
    connect(actConnect, SIGNAL(clicked()), this, SLOT(connectErl()));
    connect(actCancel, SIGNAL(clicked()), this, SLOT(cancel()));
//! [layout]

//![setting the layout]
    setLayout(mainLayout);
    setWindowTitle(tr("Erlang Connector"));

    setTabOrder(actConnect,name);
    setTabOrder(name,host);
    setTabOrder(host,cookie);
    setTabOrder(host,actCancel);
}

void Connector::createMeadow(int number)
{
    common->meadow = new Meadow(number, number);
    for (int i = 0; i < number; ++i)
        for (int j = 0; j < number; ++j)
            common->meadow->setField(i,j,0);

    char buffer[20];
    sprintf(buffer,"{size, %d, %d}", number, number);
    myerl_commander(myerl,buffer);
    myerl_commander(myerl,"init");
}

void Connector::play()
{
    myerl_commander(myerl,"play");
}

void Connector::pause()
{
    myerl_commander(myerl,"pause");
}

void Connector::stop()
{
    myerl_commander(myerl,"stop");
}

int Connector::connectErl()
{
    std::string cookiec = (cookie->text()).toStdString();
    std::string namec = (name->text()).toStdString();
    std::string hostc = (host->text()).toStdString();
    if (myerl_init(myerl, cookiec, namec, hostc) == 0)
    {
        if (myerl_launch_listener(myerl) == 0)
        {
            saveDefault(cookiec, namec, hostc);
            char buffer[30];
            sprintf(buffer,"{host, c1%s}", hostc.c_str());
            myerl_commander(myerl,buffer);
            accept();
        }
        else
        {
            cError = new QErrorMessage(this);
            cError->showMessage("Nie powiodło się ustawienie nasłuchu");
        }
    }
    else
    {
        cError = new QErrorMessage(this);
        cError->showMessage("Nie powiodło się ustalenie połączenia z maszyną Erlanga");
    }
}

void Connector::cancel()
{
    reject();
}

void Connector::newThingy(const int x, const int y, const int type, const int spawnW, const int minW, const int weight, const int speed)
{
    char buffer[200];
    if  (type == 0)
        sprintf(buffer,"{the_thing, %d, %d, %d, %d, %d, %d}",x,y, spawnW, minW, weight, speed);
    else
        sprintf(buffer,"{the_monster, %d, %d, %d, %d, %d, %d}",x,y, spawnW, minW, weight, speed);
    myerl_commander(myerl,buffer);
}

void Connector::loadDefault()
{
    std::string _cookie;
    std::string _name;
    std::string _host;

    std::ifstream in(".defaults");
    if (in.is_open())
    {

        in >> _cookie;
        in >> _name;
        in >> _host;
    }
    else
    {
        _cookie = "max";
        _name = "sim";
        _host = "@localhost";
    }
    name = new QLineEdit(QString(_name.c_str()));
    cookie = new QLineEdit(QString(_cookie.c_str()));
    host = new QLineEdit(QString(_host.c_str()));
}

void Connector::saveDefault(std::string _cookie, std::string _name, std::string _host)
{
    std::ofstream out(".defaults");
    if (out.is_open())
    {
      out << _cookie << std::endl << _name << std::endl << _host << std::endl;
      out.close();
    }
}

